package com.example.work4.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Message
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.baidu.mapapi.SDKInitializer
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.example.work4.DataUtil
import com.example.work4.R
import com.example.work4.baiduMapSDK.ZoomUtil
import com.example.work4.common.FIN
import com.example.work4.http.MomentRetrofitImp
import com.example.work4.http.data.MomentPageData
import com.example.work4.po.Moments
import com.example.work4.view.MomentDialog

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [MapFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class MapFragment : Fragment() ,BaiduMap.OnMarkerClickListener{
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null
    private var mapView:MapView?=null
    var apiMoment = MomentRetrofitImp()
    var list:List<Moments> = ArrayList<Moments>()
    var map=HashMap<Marker,Moments>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }

        ZoomUtil.init();
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_map, container, false)
    }

//    val mMapStatusChangeListener  = BaiduMap.OnMapStatusChangeListener(){
//
//    }

    override fun onResume() {
        super.onResume()
        isCenter=false
        if(mapView==null) {
            SDKInitializer.initialize(activity);
            mapView = view?.findViewById(R.id.mapView)
            baiduMap=mapView?.map
            val mapSt:MapStatusUpdate = MapStatusUpdateFactory.zoomTo(18F);
            baiduMap?.setMapStatus(mapSt)
            baiduMap?.isMyLocationEnabled = true;
        }
        mapView!!.onResume()
        apiMoment.handler=mHandler
        DataUtil.mapHandler = mHandler
        apiMoment.queryMomentsInDis(114.623805000000,30.461604000000,1000.0)
    }

    var mHandler= @SuppressLint("HandlerLeak")
    object : Handler()
    {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when(msg.what)
            {
                FIN.MESSAGEOFMOMENTS->
                {
                    val data =  msg.obj as MomentPageData
                    list = data.items

                    updateMark()

                }

                DataUtil.UPDATE_MAP_MARK_LIST->
                {
                    apiMoment.queryMomentsInDis(114.623805000000,30.461604000000,1000.0)
                }
            }
        }
    }

    var curMapList = ArrayList<Marker>()
    private fun updateMark() {
        for(mark in curMapList){
           mark.remove()
        }
        curMapList.clear()
        for(i in list!!)
        {
            val latLng = LatLng(i.latitude,i.longitude)
            val bitmap = BitmapDescriptorFactory.fromResource(R.drawable.location)
            //准备 marker option 加入 marker 使用
            val markerOptions = MarkerOptions().icon(bitmap).position(latLng)
            //获取加入的 marker 这样便于兴许的操作
            val marker = baiduMap!!.addOverlay(markerOptions) as Marker
            map[marker] = i
            curMapList.add(marker)
        }
        baiduMap?.setOnMarkerClickListener(this)
    }


    override fun onMarkerClick(p0: Marker?): Boolean {
        val moments=map.get(p0)
        val myDialog = context?.let { moments?.let { it1 ->
            MomentDialog(it1, it  )
        } }
        myDialog?.show()
        return true
    }

    override fun onPause() {
        mapView!!.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        mapView!!.onDestroy()
        super.onDestroy()
    }

    companion object {
        var baiduMap:BaiduMap?=null
        var isCenter:Boolean=false
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment MapFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            MapFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

}